/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.firstlife.mps.unittests;

import j2meunit.framework.Test;
import j2meunit.framework.TestCase;
import j2meunit.framework.TestMethod;
import j2meunit.framework.TestSuite;

import java.util.Vector;

import com.aroundj2me.location.LocationController;
import com.aroundj2me.model.Communicator;
import com.aroundj2me.model.Constants;
import com.aroundj2me.model.Location;
import com.aroundj2me.model.Locator;
import com.aroundj2me.model.Place;
import com.aroundj2me.xml.XMLDecoder;
import com.aroundj2me.xml.XMLEncoder;

public class CommunicatorTest extends TestCase {
	private Communicator comm;
	private XMLEncoder encoder;
	private XMLDecoder decoder;
	private final String username = "kito";
	private final String password = "mypass";

	public CommunicatorTest(String name, TestMethod method) {
		super(name, method);
	}

	public CommunicatorTest() {
	}

	protected void setUp() throws Exception {
		comm = new Communicator("http://localhost:8080/aroundME/server");
		encoder = new XMLEncoder();
		decoder = new XMLDecoder();
	}

	protected void tearDown() throws Exception {
		comm = null;
		encoder = null;
		decoder = null;
	}
	
	public void testCommitPlace(){
		Place p = new Place();
		p.setId(13);
		p.setName("Pizzeria Torcia");
		p.setAddress("Centro ...");
		p.setCity("Bolzano");
		p.setPhone("0471 934213");
		p.setUser("kito");
		
		//cannot pass reference of AroundMe midlet since this causes a "Restricted API" exception
		LocationController lController = new LocationController(null);	
		Location l = lController.getCurrentLocation();
		p.setLocation(l);
		
		
		String encodedXML = encoder.encodePlaceToXML(p, username, password);
		
		comm.setRequestXML(encodedXML);
		comm.run();
		
		String returnedXML = comm.getResponseXML();
		assertNotNull(returnedXML);
		
		decoder.setXmlResponseString(returnedXML);
		try {
			decoder.parseXML();
		} catch (Exception e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
		
		assertEquals("type of response should be an alert", Constants.ALERT, decoder.getType());
		assertEquals("Type of the alert should be info", Constants.ALERT_INFO, decoder.getAlert().getTitle());
	}
	
	public void testRetrievePlaceAll(){
		Place p = new Place();
		p.setName("Pizzeria Torcia"+System.currentTimeMillis());
		p.setAddress("Centro ...");
		p.setCity("Bolzano");
		p.setPhone("0471 934213");
		p.setUser("kito");
		Location l = new Locator().getLocation();
		p.setLocation(l);
		
		
		String encodedXML = encoder.encodePlaceToXML(p, username, password);
		
		comm.setRequestXML(encodedXML);
		comm.run();
		
		String encodedRetrieveXML = encoder.encodeRequest(Constants.PARAM_RETRIEVE_GLOBAL,0, 9999999, username, password);
		comm.setRequestXML(encodedRetrieveXML);
		comm.run();
		
		String returnedXml = comm.getResponseXML();
		assertNotNull(returnedXml);
		
		decoder.setXmlResponseString(returnedXml);
		try {
			decoder.parseXML();
		} catch (Exception e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
		
		Vector places = decoder.getPlaces();
		assertNotNull(places);
		
		boolean committedPlaceFound = false;
		for(int i=0; i<places.size(); i++){
			Place tmp = (Place) places.elementAt(i);
			if(tmp.getName().equals(p.getName())){
				committedPlaceFound = true;
			}
		}
		
		assertTrue(committedPlaceFound);
	}

	public Test suite() {
		TestSuite suite = new TestSuite();
		suite.addTest(new CommunicatorTest("CommunicatorTest:testCommitPlace", new TestMethod() {
			public void run(TestCase tc) {
				((CommunicatorTest) tc).testCommitPlace();
			}
		}));
		suite.addTest(new CommunicatorTest("CommunicatorTest:testRetrievePlaceAll", new TestMethod() {
			public void run(TestCase tc) {
				((CommunicatorTest) tc).testRetrievePlaceAll();
			}
		}));
		return suite;
	}
}
